package com.jintu.portal.service.datadriver.parser.impl;

import com.alibaba.fastjson.JSONObject;
import com.jintu.portal.service.datadriver.parser.DataParser;
import com.jintu.portal.service.datadriver.repository.SourceDataMongoDao;
import com.jintu.portal.pojo.submit.DataResponse;
import com.jintu.portal.service.datadriver.parser.ParserCommon;
import com.jintu.portal.service.datadriver.pojo.MsgRespCode;
import com.jintu.portal.service.datadriver.pojo.RespInfo;
import com.jintu.portal.service.datadriver.pojo.Response;
import com.jintu.portal.service.datadriver.pojo.SysRespCode;
import com.jintu.portal.service.datadriver.pojo.entity.SourceData;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.bson.Document;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author very
 */
@Slf4j
@Component
public class DuoTouParser implements DataParser {

    @Resource(name = "sourceDataMongoDao")
    private SourceDataMongoDao sourceDataMongoDao;

    @Override
    public boolean parseAndSaveData(String data, DataResponse dataResponse, Integer userId) {
        if (StringUtils.isBlank(data)) {
            return ParserCommon.isDataSourceIsEmpty(dataResponse);
        }
        Response response = JSONObject.parseObject(data, Response.class);
        String retCode = response.getRetCode();
        if (SysRespCode.QUERY_SUCCESS.getCode().equals(retCode)) {
            RespInfo respInfo = response.getRetInfo();
            ParserCommon.wrapSyncDataSuccess(dataResponse);
            if(MsgRespCode.SEARCH.getCode().equals(respInfo.getCode())){
                String resultData = ((JSONObject)respInfo.getMessage()).getString("result_detail");
                Document savingData = new Document();
                JSONObject resultDetail;
                if(StringUtils.isBlank(resultData)){
                    log.error("【{}】信用行为雷达返回result_detail为空, 未命中", JSONObject.toJSONString(dataResponse));
                    resultDetail = new JSONObject();
                    resultDetail.put("loans_count", "0");
                    resultDetail.put("loans_long_time", "0");
                    resultDetail.put("consfin_org_count", "0");
                    resultDetail.put("latest_six_month", "0");
                    resultDetail.put("loans_cash_count", "0");
                    resultDetail.put("history_fail_fee", "0");
                    resultDetail.put("latest_three_month", "0");
                    resultDetail.put("latest_one_month_fail", "0");
                    resultDetail.put("latest_one_month", "0");
                    resultDetail.put("latest_one_month_suc", "0");
                    resultDetail.put("loans_latest_time", "");
                    resultDetail.put("history_suc_fee", "0");
                    resultDetail.put("loans_org_count", "0");
                    resultDetail.put("loans_credibility", "0");
                    resultDetail.put("loans_score", "0");
                    resultDetail.put("loans_overdue_count", "0");
                    resultDetail.put("loans_settle_count", "0");
                } else {
                    resultDetail = JSONObject.parseObject(resultData);
                }
                savingData.putAll(resultDetail);
                SourceData syncData = ParserCommon.wrapSourceSavingData(dataResponse, userId, savingData);
                sourceDataMongoDao.saveSourceData(syncData, dataResponse.getDataSourceCode());
                return true;
            } else {
                return false;
            }
        } else {
            dataResponse.setResponseJson(JSONObject.parseObject(JSONObject.toJSONString(response)));
            return false;
        }
    }
}
